package com.gitee.wsl.compose.chart.sample

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.filled.ArrowForward
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.gitee.wsl.compose.chart.speedometer.AwesomeSpeedometer
import com.gitee.wsl.compose.chart.speedometer.PointerSpeedometer
import com.gitee.wsl.compose.chart.speedometer.SpeedView
import com.gitee.wsl.compose.chart.speedometer.SpeedometerType
import com.gitee.wsl.compose.chart.speedometer.TubeSpeedometer
import kotlin.random.Random


val speedometerSampleView = object : SampleView {
    override val name: String = "Pie Chart"

    override val thumbnail = @Composable {
        SpeedView(
            modifier = Modifier.fillMaxSize(),
            speed = 60f,
        )
    }

    override val content: @Composable () -> Unit = @Composable {
        Speedometer()
    }
}

@Composable
fun Speedometer() {

    var speed by remember { mutableStateOf(0f) }
    val currentSpeed by animateFloatAsState(
        targetValue = speed,
        animationSpec = tween(durationMillis = 2000, easing = FastOutSlowInEasing)
    )

    var speedometer by rememberSaveable { mutableStateOf(SpeedometerType.SpeedView) }

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(12.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            IconButton(
                enabled = SpeedometerType.entries.indexOf(speedometer) > 0,
                onClick = {
                    speedometer =
                        SpeedometerType.entries.toTypedArray()[SpeedometerType.entries.indexOf(speedometer) - 1]
                },
            ) {
                Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "Previous")
            }
            Spacer(modifier = Modifier.width(8.dp))
            when (speedometer) {
                SpeedometerType.SpeedView -> {
                    SpeedView(
                        modifier = Modifier.size(250.dp),
                        speed = currentSpeed,
                    )
                }

                SpeedometerType.AwesomeSpeedometer -> {
                    AwesomeSpeedometer(
                        modifier = Modifier.size(250.dp),
                        speed = currentSpeed,
                    )
                }

                SpeedometerType.PointerSpeedometer -> {
                    PointerSpeedometer(
                        modifier = Modifier.size(250.dp),
                        speed = currentSpeed,
                    )
                }

                SpeedometerType.TubeSpeedometer -> {
                    TubeSpeedometer(
                        modifier = Modifier.size(250.dp),
                        speed = currentSpeed,
                    )
                }
            }
            Spacer(modifier = Modifier.width(8.dp))
            IconButton(
                enabled = SpeedometerType.entries.indexOf(speedometer) < SpeedometerType.entries.toTypedArray().lastIndex,
                onClick = {
                    speedometer =
                        SpeedometerType.entries.toTypedArray()[SpeedometerType.entries.indexOf(speedometer) + 1]
                },
            ) {
                Icon(imageVector = Icons.AutoMirrored.Filled.ArrowForward, contentDescription = "Next")
            }
        }
        Spacer(modifier = Modifier.height(8.dp))
        Button(
            onClick = {
                speed = Random.nextFloat() * 100
            },
        ) {
            Text("Random speed")
        }
    }
}